var page = 0,
    num = 0;
$(function () {
    doWarranty();
})

function doWarranty() {
    page++;
    $.ajax({
        url:'/serviceOrderC/searchService.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            page:page,
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.cardBox'));
            }
        },
        success:function (data) {
            if(data.code==1){
                var cardData = data.datas.data;
                if(page==1){
                    $('.cardBox').empty();
                    if(cardData.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.cardBox'));
                    }
                }

                num = cardData.length;
                for(var i=0;i<cardData.length;i++){
                    var state = cardData[i].state?'有效':'无效'
                    $('<div class="bigBox mui-clearfix" style="margin-bottom: 10px;" id="bigBox'+cardData[i].id+'"><div class="leftBox">\n' +
                        '<div class="item">商品唯一识别码</div>\n' +
                        '<div class="item">商品名称</div>\n' +
                        '<div class="item">保修时间</div>\n' +
                        '<div class="item">数量</div>\n' +
                        '<div class="item">状态</div>\n' +
                        '</div>\n' +
                        '<div class="rightBox">\n' +
                        '<div class="item">'+cardData[i].imie+'</div>\n' +
                        '<div class="item">'+cardData[i].goodName+'</div>\n' +
                        '<div class="item">'+cardData[i].createTime+'—'+cardData[i].endTime+'</div>\n' +
                        '<div class="item">'+cardData[i].num+'</div>\n' +
                        '<div class="item">'+state+'</div>\n' +
                        '</div></div>').appendTo($('.cardBox'));
                }
            }
        }
    })
    return num;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doWarranty();
                    if (numResult < 10) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});